part of '../index.dart';

class _TopBar extends StatelessWidget {
  const _TopBar();

  Widget _buildTab(
    BuildContext context, {
    required String label,
    required String icon,
    required String imId,
  }) {
    Conversation? conversation = EMConversation.fromJson({
      'convId': imId,
      'type': 0,
    });
    var fun;
    if (imId == ImManager.likeId) {
      fun = () {
        context.read<ConversationProvider>().clearUnread(imId);
        Get.to(() => LikeNotificationPage(conversation));
      };
    } else if (imId == ImManager.commentId) {
      fun = () {
        context.read<ConversationProvider>().clearUnread(imId);
        Get.to(() => CommentNotificationPage(conversation));
      };
    } else if (imId == ImManager.giftId) {
      fun = () {
        context.read<ConversationProvider>().clearUnread(imId);
        Get.to(() => GiftsNotificationPage(conversation));
      };
    }
    return GestureDetector(
      onTap: fun,
      behavior: HitTestBehavior.opaque,
      child: Stack(
        children: [
          Column(
            children: [
              Image.asset(icon, width: 50),
              Text(
                label,
                style: const TextStyle(
                  color: Color(0xFF8D8D8D),
                  fontSize: 12,
                ),
              ),
            ],
          ),
          FutureBuilder<int>(
            future: conversation.unreadCount(),
            builder: (context, snapshot) {
              return Positioned(
                right: 2,
                top: 2,
                child: Badge(
                  isLabelVisible: (snapshot.data ?? 0) > 0,
                  backgroundColor: Colors.red,
                  label: Text('${snapshot.data}'),
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    context.watch<ConversationProvider>();
    return Padding(
      padding: const EdgeInsets.only(bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildTab(
            context,
            icon: Images.icCovLike,
            label: T.current.c_like,
            imId: ImManager.likeId,
          ),
          _buildTab(
            context,
            icon: Images.icCovChat,
            label: T.current.c_review,
            imId: ImManager.commentId,
          ),
          _buildTab(
            context,
            icon: Images.icCovGift,
            label: T.current.c_gift,
            imId: ImManager.giftId,
          ),
        ],
      ),
    );
  }
}
